#ifndef FUNCTION_HEAD
#define FUNCTION_HEAD

#include "../common/common.h"
#include "Point.h"
#include <graphics.h>

enum NodeType {
    SIN,
    COS,
    ARCSIN,
    ARCCOS,
    TAN,
    ARCTAN,
    LOG,
    LOG2,
    LOG10,
    SQRT, // 根号
    X, // 未知数
    CONSTANT, // 常数
    PLUS, // +
    MINUS, // -
    MULTIPLY, // *
    DIVIDE, // /
    POWER, // ^
    LEFT_BRACKET, // (
    RIGHT_BRACKET // )
};

struct ParseResultNode {
    NodeType type;
    double value;
    ParseResultNode *next;
};

struct FunctionCache {
    ege_point *pointCache;
    int size;
    FunctionCache *next;
};

class Function 
{
    private:
        
        FunctionCache *pCache;
    public:
        Function *next; 
        color_t functionColor;

        Function(char *equation, int size) {
            assert(size > 0);

            // TODO 解析方程

            this->next = NULL;
            this->pCache = NULL;

            // random a color
            functionColor = EGERGB(random(256), random(256), random(256));
        }

        Point calculate(double x);

        void clearCache() {
            this->pCache = NULL;
        }

        void setCache(FunctionCache *pCache) {
            this->pCache = pCache;
        }

        FunctionCache* getCache() { return pCache; }
};

ParseResultNode* parse(char *str);

#endif